﻿using System.Reactive.Linq;
using KinonekoSoftware.ArkSpark.Documents.Primitives;
using KinonekoSoftware.ArkSpark.Documents.Widgets;
using KinonekoSoftware.ArkSpark.Runtimes;
using KinonekoSoftware.ArkSpark.Runtimes.Sights;

namespace KinonekoSoftware.Studio.Pages.Composes
{
    public sealed class ComposeEditorViewModel : TabViewModelCore
    {
        private bool   _isInlineEditing;
        private bool   _isEditing;
        private bool   _isHintPanelOpen;
        private Sight  _sight;


        private DateTime _ThrottleTextChangeTimestamp;
        private DateTime _ThrottleModelChangeTimestamp;


        public ComposeEditorViewModel()
        {
            _ThrottleTextChangeTimestamp  = DateTime.Now;
            _ThrottleModelChangeTimestamp = DateTime.Now;

            Hints          = new ObservableCollection<object>();


            //
            //
            SwitchToConsultSight   = CreateCommand(DoSwitchToConsultSightCommand);
            SwitchToDedicatedSight = CreateCommand(DoSwitchToDedicatedSightCommand);
            
        }



        //-------------------------------------------------------------
        //
        //          Method:
        //          Initialize
        //
        //-------------------------------------------------------------
        private void OnTextChanged()
        {
            if ((DateTime.Now - _ThrottleTextChangeTimestamp).TotalMilliseconds <= 300)
            {
                return;
            }

            //
            // 限流
            _ThrottleTextChangeTimestamp = DateTime.Now;

            //
            SetDirtyState(true);
        }

        private void OnModelChanged()
        {

            if ((DateTime.Now - _ThrottleModelChangeTimestamp).TotalMilliseconds <= 300)
            {
                return;
            }

            //
            // 限流
            _ThrottleModelChangeTimestamp = DateTime.Now;

            //
            //
        }

        private void OnDocumentElementSelected(bool isWYSIWYGMode, DocumentElementPresenter element)
        {
            //
            // 如果是非所见即所得模式，则打开Inline面板
            if (!isWYSIWYGMode)
            {
                //
                // 非WYSIWYGMode，即类似Image、Talking等元素，这些元素都无法在
                IsInlineEditing = true;
                IsEditing       = false;
            }
            else
            {

                //
                // 通用编辑器栏目
                IsInlineEditing = false;
                IsEditing       = true;
            }
        }

        private Task OnExternalProcessing(DocumentElementPresenter element)
        {
            return new Task(() => OnDocumentElementExternalProcessing(element));
        }

        private async void OnDocumentElementExternalProcessing(DocumentElementPresenter element)
        {
            //
            // TODO: 外部编辑器
        }


        //-------------------------------------------------------------
        //
        //          Method: CommandHandlers
        //
        //-------------------------------------------------------------

        private void DoSwitchToDedicatedSightCommand()
        {
            IsConsultSight   = false;
            IsDedicatedSight = true;
            RaiseUpdated(nameof(IsConsultSight));
            RaiseUpdated(nameof(IsDedicatedSight));
        }

        private void DoSwitchToConsultSightCommand()
        {

            IsConsultSight   = true;
            IsDedicatedSight = false;
            RaiseUpdated(nameof(IsConsultSight));
            RaiseUpdated(nameof(IsDedicatedSight));
        }

        //-------------------------------------------------------------
        //
        //          Method:
        //          Initialize
        //
        //-------------------------------------------------------------
        protected override void OnStart()
        {
            base.OnStart();
        }


        //-------------------------------------------------------------
        //
        //          Method:
        //          Initialize
        //
        //-------------------------------------------------------------

        /// <summary>
        /// 获取或设置 <see cref="Sight"/> 属性。
        /// </summary>
        public Sight Sight
        {
            get => _sight;
            set => SetValue(ref _sight, value);
        }
        
        public bool IsEditing
        {
            get => _isEditing;
            set => SetValue(ref _isEditing, value);
        }

        public bool IsInlineEditing
        {
            get => _isInlineEditing;
            set => SetValue(ref _isInlineEditing, value);
        }


        public bool IsHintPanelOpen
        {
            get => _isHintPanelOpen;
            set => SetValue(ref _isHintPanelOpen, value);
        }

        public bool                         IsDedicatedSight { get; private set; }
        public bool                         IsConsultSight   { get; private set; }
        public ObservableCollection<object> Hints            { get; }

        //-------------------------------------------------------------
        //
        //    Properties:
        //           Commands
        //
        //-------------------------------------------------------------
        public ICommandEX SwitchToConsultSight   { get; }
        public ICommandEX SwitchToDedicatedSight { get; }
    }
}